<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="60%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabIndex">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules" 
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="标题" prop="title" v-if="tabIndex==1">
        <a-input v-model="form.title" placeholder="标题" />
      </a-form-model-item>
      <a-form-model-item label="英文标题" v-else>
        <a-input v-model="form.titleEn" placeholder="英文标题" />
      </a-form-model-item>
      <a-form-model-item label="子标题1" prop="subTitle1" v-if="tabIndex==1">
        <a-input v-model="form.subTitle1" placeholder="子标题1" />
      </a-form-model-item>
      <a-form-model-item label="英文子标题1" v-else>
        <a-input v-model="form.subTitle1En" placeholder="英文子标题1" />
      </a-form-model-item>
      <a-form-model-item label="子标题1电话" prop="subTitle1Phone">
        <a-input v-model="form.subTitle1Phone" placeholder="子标题1电话" />
      </a-form-model-item>
      <a-form-model-item label="子标题2" prop="subTitle1" v-if="tabIndex==1">
        <a-input v-model="form.subTitle2" placeholder="子标题2" />
      </a-form-model-item>
      <a-form-model-item label="英文子标题2" v-else>
        <a-input v-model="form.subTitle2En" placeholder="英文子标题2" />
      </a-form-model-item>
      <a-form-model-item label="子标题2电话" prop="subTitle2Phone">
        <a-input v-model="form.subTitle2Phone" placeholder="子标题2电话" />
      </a-form-model-item>
      <a-form-model-item label="排序" prop="sortNum">
        <a-input-number style="width:200px" v-model="form.sortNum" placeholder="排序" />
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>
import { insertContactWay,updateContactWay } from '@/api/manage'
import { showMessage } from '@/utils/mixins'

export default {
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  mixins: [showMessage],
  data() {
    return {
      tabIndex: '1',
      visible: false,
      title: '',
      labelCol: {span: 24},
      wrapperCol: {span: 24},
      form: {
        title: undefined,
        titleEn: undefined,
        subTitle1: undefined,
        subTitle1En: undefined,
        subTitle2: undefined,
        subTitle2En: undefined,
        subTitle1Phone: undefined,
        subTitle2Phone: undefined,
        sortNum: 0
      },
      rules: {
        title: [{required: true, message: '标题不能为空', trigger: 'blur'}],
      },
      status: undefined,
      id: undefined
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          if(this.status == 1) {
            insertContactWay({
              ...this.form
            }).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })  
          }else {
            updateContactWay({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res,() => {
                this.onClose()
                this.parent.getList()
              })
            })  
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      this.id = undefined
      this.status = undefined
      this.visible = false
      for(const key in this.form) {
        if(key == 'sortNum') {
          this.form[key] = 0
        }else {
          this.form[key] = undefined
        }
      }
    }
  }
}
</script>